package com.nwork.qingmanhua.tasks;

import android.text.TextUtils;
import com.nwork.qingmanhua.model.Entry;
import com.nwork.qingmanhua.utils.HttpTools;

import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.List;

/**
 * 解析服务器数据父类  所有的异步任务应该继承此类
 */
public abstract class BaseTask {
    private FetchEntryListener mEntryListener;
    private boolean mIsPost;
    protected Entry entry;

    /**
     * 通过此接口返回一个Entry对象
     */
    public static interface FetchEntryListener {
        public void onFetch(Entry entry);
    }

    /**
     * 异步任务的get请求
     *
     * @param listener
     */
    public void asyncGet(FetchEntryListener listener) {
        mEntryListener = listener;
        request();
    }

    /**
     * 异步任务的post请求
     *
     * @param listener
     */
    public void asyncPost(FetchEntryListener listener) {
        mEntryListener = listener;
        mIsPost = true;
        request();
    }

    private void request() {
        HttpTools.HttpRequest request = new HttpTools.HttpRequest(getUrl(),
                new HttpTools.FetchHttpDataListener() {
                    @Override
                    public void onFetch(String result) {
                        handlerJson(result);
                    }
                });
        request.setIsPost(mIsPost);
        request.setParmaList(getParamList());
        HttpTools.getInstance().request(request);
    }

    /**
     * 判断json是否有效
     *
     * @param result
     */
    private void handlerJson(String result) {
        boolean success = false;
        if (TextUtils.isEmpty(result)) {
            // do nothing...
        } else if (!result.startsWith("{") && !result.startsWith("[")) {
            // do nothing...
        } else {
            try {
                if (result.startsWith("{")) {
                    // JsonObject
                    JSONObject object = new JSONObject(result);
                    if (objectIsEmpty(object)) {
                        // do nothing...
                    } else {
                        parseJsonObject(object);
                        success = true;
                    }
                } else {
                    // JsonArray
                    JSONArray array = new JSONArray(result);
                    if (arrayIsEmpty(array)) {
                        // do nothing...
                    } else {
                        parseJsonArray(array);
                        success = true;
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        if (mEntryListener != null) {
            mEntryListener.onFetch(success ? entry : null);
        }
    }

    /**
     * 解析jsonObject
     */
    protected abstract void parseJsonObject(JSONObject object);

    /**
     * 解析jsonArray
     */
    protected abstract void parseJsonArray(JSONArray array);

    protected abstract String getUrl();

    protected List<NameValuePair> getParamList() {
        return null;
    }


    /**
     * 判断得到的JsonObject 是否为空
     *
     * @param object
     * @return
     */
    protected boolean objectIsEmpty(JSONObject object) {
        return object == null || JSONObject.NULL.equals(object);
    }

    /**
     * 判断返回的JsonArray是否为空
     *
     * @param array
     * @return
     */
    protected boolean arrayIsEmpty(JSONArray array) {
        return array == null || array.length() == 0;
    }
}
